import axios from 'axios';
function ajax(url = '', data = {}, type = 'GET') {
  return new Promise(function (resolve, reject) {
    let promise;
    if (type === 'GET') {
      // 准备url query参数数据
      let dataStr = ''; // 数据拼接字符串
      console.log('223');
      Object.keys(data).forEach(key => {
        dataStr += key + '=' + data[key] + '&';
      });
      console.log('333');
      if (dataStr !== '') {
        dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
        url = url + '?' + dataStr;
      }
      // 发送get请求
      promise = axios.get(url);
    } else {
      // 发送post请求
      promise = axios.post(url, data);
    }

    promise.then(response => {
      resolve(response.data);
    })
      .catch(error => {
        reject(error);
      });
  });
}
export default ajax;
